package com.longge;

import java.util.HashMap;

/**
 * @program: my-code
 * @description:
 * @author: longge
 * @create: 2021-04-16 01:29
 */
public class CopyRandomList1 {

    public Node copyRandomList(Node head) {

        if (head == null) return null;
        HashMap<Node, Node> map = new HashMap<>();
        Node cur = head,h = head;
        while (cur != null) {
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }
        while (head!=null){
            Node node = map.get(head);
            node.next=map.get(head.next);
            node.random=map.get(head.random);
            head=head.next;
        }
        return map.get(h);
    }

}
